package com.rongchuang.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.rongchuang.dto.RoleDTO;
import com.rongchuang.entity.QRole;
import com.rongchuang.entity.Role;
import com.rongchuang.repository.RoleRepository;
import com.rongchuang.util.ConverterUtil;
import com.rongchuang.util.DateUtils;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class RoleService {

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private JPAQueryFactory queryFactory;

    /**
     * 分页查询
     */
    public Page<RoleDTO> pageSearch(int pageNum, int pageSize, String name) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        QRole role = QRole.role;
        BooleanBuilder builder = new BooleanBuilder();
        if (StringUtils.isNotBlank(name)) {
            builder.and(role.roleName.eq(name));
        }
        List<Role> roleList = queryFactory
                .selectFrom(role)
                .where(builder)
                .offset((long) pageNum * pageSize)
                .limit(pageSize)
                .orderBy(role.id.desc())
                .fetch();
        long total = queryFactory
                .selectFrom(role)
                .where(builder)
                .fetchCount();
        List<RoleDTO> roleDTOS = ConverterUtil.convertToList(roleList, RoleDTO.class);
        return new PageImpl<>(roleDTOS, pageable, total);
    }

    /**
     * 保存角色信息
     */
    public void modify(RoleDTO roleDTO) {
        Role role;
        if (Objects.isNull(roleDTO.getId())) {
            role = ConverterUtil.convertTo(roleDTO, Role.class);
            role.setCreateTime(DateUtils.getCurrentTime());
        }else {
            role = roleRepository.getById(roleDTO.getId());
            if (StringUtils.isNotBlank(roleDTO.getRoleName())) {
                role.setRoleName(roleDTO.getRoleName());
            }
            if (StringUtils.isNotBlank(roleDTO.getDescription())) {
                role.setDescription(roleDTO.getDescription());
            }
        }
        role.setUpdateTime(DateUtils.getCurrentTime());
        roleRepository.save(role);
    }

    /**
     * 获取角色详情
     * @param id 角色id
     * @return 角色详情
     */
    public RoleDTO getDetail(Long id) {
        Role role = roleRepository.getById(id);
        return ConverterUtil.convertTo(role, RoleDTO.class);
    }

    /**
     * 获取角色列表
     * @param associationId 商会id
     * @return 角色列表
     */
    public List<RoleDTO> findByAssociationId(Long associationId) {
        List<Role> roles = roleRepository.findByAssociationId(associationId);
        return ConverterUtil.convertToList(roles, RoleDTO.class);
    }
}
